package com.translate.web.service.impl;

import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.ServicePriceConfMapper;
import com.common.util.ListConvertMap;
import com.translate.entity.ServicePriceConf;
import com.translate.vo.contract.IServicePriceConfVO;
import com.translate.vo.contract.IServiceTypeVO;
import com.translate.web.service.IServicePriceConfService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;

import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Service;


@Service
public class ServicePriceConfServiceImpl extends BaseTranslateServiceImpl<ServicePriceConf> 
	implements IServicePriceConfService {
	public ServicePriceConfServiceImpl(ServicePriceConfMapper mapper) {
		super(mapper);
	}

	protected void insertPropteris(ServicePriceConf data) {
		long time = System.currentTimeMillis() / 1000;
		data.setCreateTime(time);
		data.setUpdateTime(time);
	}

	protected void updatePropteris(ServicePriceConf data) {
		data.setUpdateTime(System.currentTimeMillis() / 1000);
	}

	protected Class<ServicePriceConf> getEntityClass() {
		return ServicePriceConf.class;
	}

	@Override
	public <VO extends IServicePriceConfVO> List<VO> getListFromCache(Class<VO> target) {
		return this.entityConvertToVo(this.listToHash(RedisConstant.SERVICE_CONF, 100), target);
	}
	

	@Resource
	public void setRedisTemplate(RedisTemplate<String, List<ServicePriceConf>> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	@Override
	public Map<Integer, ServicePriceConf> getListFromCache() {
		return getServicePriceConf();
	}
	
	@Override
	public Map<Integer, List<ServicePriceConf>> getMapListFromCache() {
		return getServicePriceConfList();
	}

	private Map<Integer, ServicePriceConf> getServicePriceConf() {
		HashOperations<String, String, ServicePriceConf> hashOperations = redisTemplate.opsForHash();
		
		Long size = hashOperations.size(RedisConstant.SERVICE_CONF);
		
		Map<Integer, ServicePriceConf> listToMap;
		
		if (0 == size) {
			List<ServicePriceConf> dataToHash = this.dataToHash(RedisConstant.SERVICE_CONF, 100, hashOperations);
			listToMap = ListConvertMap.listToMap(dataToHash, ServicePriceConf::getServiceId);
		} else {
			
			ScanOptions scanOptions = ScanOptions.scanOptions().build();
			
			Cursor<Entry<String, ServicePriceConf>> scan = hashOperations.scan(RedisConstant.SERVICE_CONF, scanOptions);
			
			listToMap = new HashMap<>(36);
			
			while(scan.hasNext()) {
				Entry<String, ServicePriceConf> next = scan.next();
				
				listToMap.put(Integer.valueOf(next.getKey()), next.getValue());
			}
		}
		
		
		return listToMap;
	}
	
	private Map<Integer, List<ServicePriceConf>> getServicePriceConfList() {
		HashOperations<String, String, ServicePriceConf> hashOperations = redisTemplate.opsForHash();
		
		Long size = hashOperations.size(RedisConstant.SERVICE_CONF);
		
		Map<Integer, List<ServicePriceConf>> listToMap;
		
		if (0 == size) {
			List<ServicePriceConf> dataToHash = this.dataToHash(RedisConstant.SERVICE_CONF, 100, hashOperations);
			listToMap = ListConvertMap.listToMapList(dataToHash, ServicePriceConf::getServiceId);
		} else {
			
			ScanOptions scanOptions = ScanOptions.scanOptions().build();
			
			Cursor<Entry<String, ServicePriceConf>> scan = hashOperations.scan(RedisConstant.SERVICE_CONF, scanOptions);
			
			listToMap = new HashMap<>(36);
			
			ServicePriceConf value;
			List<ServicePriceConf> list;
			while(scan.hasNext()) {
				Entry<String, ServicePriceConf> next = scan.next();
				
				value = next.getValue();
				
				Integer serviceId = value.getServiceId();
				
				list = listToMap.get(serviceId);
				
				if (null == list) {
                    list = new ArrayList<>();
                    listToMap.put(serviceId, list);
				}
				
				list.add(value);
			}
		}
		
		return listToMap;
	}
	

	@Override
	protected int getId(ServicePriceConf data) {
		// TODO Auto-generated method stub
		return data.getId();
	}
	
	
}